

package com.bei_en.teacher.management.controller.article;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bei_en.teacher.admin.model.article.condition.ArticleTypeCondition;
import com.bei_en.teacher.admin.model.article.vo.ArticleTypeVo;
import com.bei_en.teacher.admin.service.article.ArticleTypeService;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:21:09
 */
@Controller
@RequestMapping("/admin")
public class ArticleTypeController extends BaseController {

    @Autowired
    ArticleTypeService articleTypeService;
    private void addSharedAttr(Model model) {
    	
    }

    @RequestMapping("/articleType")
    public String articleType(Model model,ArticleTypeCondition vo, HttpServletResponse resp ) {

		List<ArticleTypeVo> list =new ArrayList<ArticleTypeVo>();
		try {
		    list = articleTypeService.findArticleTypePage(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
	
		model.addAttribute("list", list);
		addSharedAttr(model);
		model.addAttribute("vo", vo);
	
		return "/admin/article/typeList.html";
    }

    @RequestMapping("/articleTypeAdd")
    public String articleTypeAdd(Model model, ArticleTypeVo vo, boolean show, HttpSession session,
	    HttpServletResponse resp) {

	if (show) {
	    addSharedAttr(model);
	    return "/admin/article/typeAdd.html";
	}
	try {
	   /* SysUserVo obj = getLoginUser(session);
	    if (obj!=null)
		vo.setCreateId(obj.getId());
	    else
		vo.setCreateId(0);*/
	    vo.setCreateTime(new Date());
	articleTypeService.insert(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}
	MyJsonSuccessResult re = new MyJsonSuccessResult();
	re.setCloseCurrent(true);
	re.setTabid("articleTypeList");
	writeJson(resp, re);
	return null;
    }

    @RequestMapping("/articleTypeEdit")
    public String articleTypeEdit(Model model,boolean show, ArticleTypeVo vo, HttpSession session, HttpServletResponse resp) {
	
		if (show) {
		    ArticleTypeVo vo1 = articleTypeService.findById(vo.getId());
		    addSharedAttr(model);
		    model.addAttribute("vo", vo1);
		    return "/admin/article/typeEdit.html";
		}
		try {
		   /* SysUserVo obj = getLoginUser(session);
		    if (obj!=null)
			vo.setUpdateId(obj.getId());
		    else
			vo.setUpdateId(0);*/
//		    vo.setUpdateTime(new Date());
		articleTypeService.updateSelective(vo);
		} catch (Exception e) {
		    e.printStackTrace();
		    writeJson(resp, new MyJsonFailResult(e.getMessage()));
		    return null;
		}
		MyJsonSuccessResult re = new MyJsonSuccessResult();
		re.setCloseCurrent(true);
		re.setTabid("articleTypeList");
		writeJson(resp, re);
		return null;
    }

    @RequestMapping("/articleTypeDel")
    @ResponseBody
    public MyJsonResult articleTypeDel(Model model, int[] delids, HttpSession session) {

		try {
		    for (int i : delids) {
			ArticleTypeVo vo1 = new ArticleTypeVo();
			vo1.setId(i);
//			vo1.setStatus(0);
			/*SysUserVo obj = getLoginUser(session);
			if (obj!=null)
			    vo1.setUpdateId(obj.getId());
			else
			    vo1.setUpdateId(0);*/
//			vo1.setUpdateTime(new Date());
//			articleTypeService.updateSelective(vo1);
			articleTypeService.delete(i);
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		    return new MyJsonFailResult(e.getMessage());
		}
		return new MyJsonSuccessResult();
	}
    
}
